// pages/user-hyzx-bdsj/user-hyzx-bdsj.js
var util = require('../../utils/network.js')
var uuidv1 = require('../../utils/node-uuid/uuidv1');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getcode: '获取验证码',
    tel:'',
    telPhonenum:'',
    imgurl: '',//图片验证码
    uid: '',
    imgcode: '',//输入图片验证码
    numcode: '',//输入数字验证码
    message: '',//错误信息
    bShow: false,//错误信息显示隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tel=JSON.parse(wx.getStorageSync('userInfo')).phonenum
    var that=this
    that.setData({
        telPhonenum:tel,
        tel:tel
    })
    var uid = uuidv1()
    that.setData({
        uid: uid
    })
    console.log(that.data.uid)
    //函数调用  
    wx.request({
        url: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS',
        data: {
            "uuid": uid,
            "devicetoken": "",
            "clienttoken": "",
            "clienttype": "weix",
            "version": "1.0"
        },
        header: {
            'content-type': ' text/html'
        },
        method: "GET",
        dataType: "json",
        success: function (res) {
            that.setData({
                imgurl: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS?uuid=' + uid
            })
        }
    })
  },
  //   输入手机号
  tel: function (e) {
      this.setData({
          tel: e.detail.value
      })
  },
  //   输入图片验证码
  imgCode: function (e) {
      this.setData({
          imgcode: e.detail.value
      })
  },
  //   输入6位数字
  numCode: function (e) {
      this.setData({
          numcode: e.detail.value
      })
  },
  //   改变验证码图片
  changeImg: function () {
      var that = this
      var uid = uuidv1()
      that.setData({
          uid: uid
      })
      //函数调用  
      wx.request({
          url: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS',
          data: {
              "uuid": uid,
              "token": {
                  "devicetoken": "",
                  "clienttoken": "",
                  "clienttype": "weix",
                  "version": "1.0"
              }
          },
          method: "GET",
          dataType: "json",
          success: function (res) {
              that.setData({
                  imgurl: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS?uuid=' + uid
              })
          }
      })
  },
  //   获取验证码
  getcode: function () {
      var that = this
      var uid = that.data.uid
      console.log(that.data.uid)
      var tel = that.data.tel
      var imgcode = that.data.imgcode
      var openId = wx.getStorageSync("openId")
      var token = util.token
      if (tel == '') {
          that.setData({
              message: '请输入正确的手机号',
              bShow: true
          })
      } else if (imgcode == '') {
          that.setData({
              message: '请输入正确的图片验证码',
              bShow: true
          })
      } else if (tel != '' && imgcode != '') {
          wx.request({
              url: util.main + '/user/userSendMessToBindOrChangePhone',
            //   "phone=" + that.data.tel + "&type=QQ&openid=" + openId + "&deviceid=" + openId + "&randomverifyCode=" + that.data.imgcode + "&token=" + token + "&uuid=" + uid,
              data: {
                  "deviceid":wx.getStorageSync('openId'),
                  "userid": wx.getStorageSync('userId'),
                  "phonenum": tel,
                  "randomverifyCode": imgcode,
                  "uuid": uid,
                  "token": token
              },
              header: {
                  "Content-Type": "application/x-www-form-urlencoded",
                  "accept":"application/json,"
              },
              method: "POST",
              success: function (res) {
                  console.log(res)
                  if (res.statusCode == 200) {
                      var wait = 181
                      var time = setInterval(function () {
                          wait -= 1
                          that.setData({
                              getcode: wait + 's',
                              disabled: true
                          })
                          if (wait < 1) {
                              wait == 10
                              clearInterval(time)
                              that.setData({
                                  getcode: '获取验证码',
                                  disabled: false
                              })
                          }
                      }, 1000)
                      that.setData({
                          message: res.data.message,
                          bShow: true
                      })
                  } else {
                      that.setData({
                          message: res.data[0].message,
                          bShow: true
                      })
                  }
                  if (that.data.bShow) {
                      setTimeout(function () {
                          that.setData({
                              bShow: false
                          })
                      }, 2000)
                  }
              }
          })
      }
      if (that.data.bShow) {
          setTimeout(function () {
              that.setData({
                  bShow: false
              })
          }, 2000)
      }
  },
  submit:function(){
      var that=this
      var numcode = that.data.numcode
      var phonenum = that.data.tel
      var token = util.token
      wx.request({
          url: util.main + '/user/checkphoneverifycode',
          data: {
              "userid": wx.getStorageSync('userId'),
              "phonenum": phonenum,
              "verifycode": numcode,
              "deviceid": wx.getStorageSync('openId'),
              "token": token
          },
          header: {
              "content-type": "application/x-www-form-urlencoded",
              "accept": "application/json,",
          },
          method: "POST",
          dataType: "json",
          success: function (res) {
              if (res.statusCode == 200) {
                  that.setData({
                      message: res.data[0].message,
                      bShow: true
                  })
                  wx.redirectTo({
                      url: '../user-hyzx/user-hyzx',
                  })
              }
              console.log(res)
              if (that.data.bShow) {
                  setTimeout(function () {
                      that.setData({
                          bShow: false
                      })
                  }, 2000)
              }
          }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})